/**
 * 合同清单contractDetail.js
 * 版本号： v1.0.0
 */
$(function () {
    var coId = $("#coId").val();
    var lastChecked = null;
    $("#contractDetail_dataList").datagrid({
        fitColumns: true,
        fit: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        pageSize: 10,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/contractDetail/contractDetailList.do?coId='+coId,
        idField:'cdId',
        frozenColumns: [[
            {field:'cdId', checkbox: true}
        ]],
        columns: [
            [
                {field: 'coNumber', title: '合同编号', width: 100, fixed: true},
                {field: 'materialName', title: '材料名称', width: 100, fixed: true},
                {field: 'brand', title: '材料品牌', width: 100, fixed: true},
                {field: 'standardType', title: '规格型号', width: 100, fixed: true},
                {field: 'technicalParameter', title: '技术参数', width: 100, fixed: true},
                {field: 'totalCount', title: '工程总量', width: 100, fixed: true},
                {field: 'unitContractPrice', title: '合同单价', width: 100, fixed: true},
                {field: 'unitPrice', title: '综合单价', width: 100, fixed: true},
                {field: 'characterization', title: '项目特性描述', width: 100, fixed: true},
                {field: 'totalPrice', title: '合计金额', width: 100}
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#contractDetail_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#contractDetail_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#contractDetail_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#contractDetail_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });
});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //删除按钮
                if("delBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.cdId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            contractDetail_del();
                        }
                    });
                }
                //修改按钮
                if("editBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            contractDetail_edit();
                        }
                    });
                }
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.cdId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            contractDetail_findList();
                        }
                    });
                }
                //查看详细按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.cdId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            contractDetail_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
                //返回合同清单按钮
                if("returnBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.cdId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            //跳转清单列表
                            var goUrl = CONTEXT_PATH + "/contractDetail/index.do";
                            var goTitle = "合同清单";
                            var iconCls = "";
                            goback(goTitle, goUrl, iconCls, true);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 删除合同清单
 */
function contractDetail_del() {
    var row = $("#contractDetail_dataList").datagrid("getSelected");
    var checked = $("#contractDetail_dataList").datagrid("getChecked");
    if (!checked || !checked.length) {
        $.messager.show({title: '操作提示', msg: "请选择删除合同清单", timeout: 3000, showType: "slide"});
        return;
    }
    $.messager.confirm('删除提示', '您确定要删除' + row.materialName + '材料的清单吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/contractDetail/del.do?cdId=" + row.cdId,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#contractDetail_dataList").datagrid("reload", {
                        prName:$("#prName_").val(),
                        coNumber:$("#coNumber").val(),
                        coName:$("#coName").val(),
                        materialName:$("#materialName").val(),
                        standardType:$("#standardType").val()
                    });
                }
            })
        }
    });
}

/**
 * 修改合同清单
 * @returns {boolean}
 */
function contractDetail_edit() {
    var row = $("#contractDetail_dataList").datagrid("getSelected");
    var checked = $("#contractDetail_dataList").datagrid("getChecked");
    if (!checked || !checked.length) {
        $.messager.show({title: '操作提示', msg: "请选择修改合同清单", timeout: 3000, showType: "slide"});
        return;
    }
    window.location.href = CONTEXT_PATH + "/contractDetail/toContractDetail.do?cdId=" + row.cdId;
}

//查询合同清单
function contractDetail_findList() {
    refreshPageNumber("contractDetail_dataList");
    $("#contractDetail_dataList").datagrid("reload",{
        materialName:$("#materialName").val(),
        standardType:$("#standardType").val()
    });
    //清楚选中项
    $("#contractDetail_dataList").datagrid("clearSelections");
}

/***
 * 动态刷新页面
 * @param coId
 */
function refreshPageNumber(coId) {
    //获取dataGrid的列表对象属性
    var $datagrid = $("#" + coId).datagrid("options");
    if ($datagrid != undefined) {
        $datagrid.pageNumber = 1;
    }
    //获取dataGrid的分页对象
    var $getPager = $("#" + coId).datagrid('getPager');
    var $pagination = $($getPager).pagination("options");
    if ($pagination != undefined) {
        $pagination.pageNumber = 1;
    }
}

/**
 * 查看合同清单详情
 */
function contractDetail_view(iconCls){
    var row = $("#contractDetail_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的合同清单详情", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(htqd.id,htqd.url + row.cdId+"&&coId="+row.coId, iconCls, htqd.closable,htqd.title);
}
